package com.flute.tools.xml.xpath;

import java.util.HashMap;

public class GroupElementPathNode implements IPathNode {
	public static final String SPLITER = "\\|";

	private IPathNode group;
	private IPathNode pathNode;

	public GroupElementPathNode() {
	}

	public GroupElementPathNode(MixedPathNode group, IPathNode pathNode) {
		this.group = group;
		this.pathNode = pathNode;
	}

	public IPathNode getGroup() {
		return group;
	}

	public void setGroup(IPathNode group) {
		this.group = group;
	}

	public IPathNode getPathNode() {
		return pathNode;
	}

	public void setPathNode(IPathNode pathNode) {
		this.pathNode = pathNode;
	}

	@Override
	public GroupElementPathNode fromPathString(String path)
			throws XmlPathException {
		GroupElementPathNode node = null;

		if (path != null) {
			String[] ps = path.split(SPLITER);

			if (ps.length == 2) {
				node = new GroupElementPathNode();
				node.group = PathParsers.getInstance().parse(ps[0]);
				node.pathNode = PathParsers.getInstance().parse(ps[1]);
			}

		}
		
		return node;
	}

	@Override
	public String toPathString() {
		return group.toPathString() + SPLITER + pathNode.toPathString();
	}

	@Override
	public PathNodes[] divider(PathNodes nodes) {
		HashMap<String, PathNodes> map = new HashMap<String, PathNodes>();

		for (IPathNode node : nodes.getPathNodes()) {
			if (node instanceof GroupElementPathNode) {
				GroupElementPathNode gepn = (GroupElementPathNode) node;
				String key = gepn.getGroup().toPathString();
				if (!map.containsKey(key)) {
					map.put(key, new PathNodes());
				}

				map.get(key).addPathNode(gepn);
			}
		}

		return map.values().toArray(new PathNodes[] {});
	}

}
